package com.wzy.flyweight;

import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: wzy
 * @Date: 2021/12/05/14:17
 * @Description: 复合享元角色实现了抽象享元角色所规定的接口，也就是operation()方法，这个方法有一个参数，代表复合享元对象的外蕴状态。
 * 一个复合享元对象的所有单纯享元对象的外蕴状态都是与复合享元对象的外蕴状态相等的；
 * 而一个复合享元对象所含有的单纯享元对象的内蕴状态一般是不相等的，不然就没有使用价值了。
 */
public class UnsharedConcreteFlyweight implements Flyweight {

    //用来登记和存储已经创建过的享元对象
    private Map<String, Flyweight> flyweightMap = new HashMap<String, Flyweight>();

    //指定所有享元对象的外蕴状态
    private String extrinsicState = null;

    // 增加一个新的单纯享元对象到集合中
    public void add(String intrinsicState, Flyweight flyweight) {
        flyweightMap.put(intrinsicState, flyweight);
    }

    @Override
    public void operation(String extrinsicState) {
        this.extrinsicState = extrinsicState;
    }

    public void getFlyweight(String intrinsicState){
        Flyweight flyweight=flyweightMap.get(intrinsicState);
        if(null!=flyweight){
            flyweight.operation(extrinsicState);
        }
        else{
            System.out.println();
            System.out.println(extrinsicState+"未点过:"+intrinsicState);
            System.out.println("-------------------------");
        }
    }

    public Map<String, Flyweight> getFlyweightMap() {
        return flyweightMap;
    }
}
